package com.SpringMVC.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author:liuwei
 * @creatdate:2022-03-02-16:18
 */
@Controller
public class ScopeController {
	//使用ServletAPI向request域对象共享数据
	@RequestMapping("/testRequestServletAPI")
	public String testRequestServletAPI(HttpServletRequest request){
		request.setAttribute("testRequestScope","hello,servletApi");
		return "success";
	}

	@RequestMapping("/testModelAndView")
	public ModelAndView testModelAndView(){
		ModelAndView modelAndView = new ModelAndView();
		//处理模型数据，向请求域request共享数据
		modelAndView.addObject("testRequestScope1","hello,ModelAndView");
		//设置视图名称
		modelAndView.setViewName("success");
		return modelAndView;
	}

	@RequestMapping("/testModel")
	public String testModel(Model model) {
		model.addAttribute("testRequestScope2","hello,Model");
		return "success";
	}
	@RequestMapping("/testMap")
	public String testMap(Map<String,Object> map){
		map.put("testRequestScope3","hello,map");
		return "success";
	}

	@RequestMapping("/testModelMap")
	public String testModel(ModelMap model) {
		model.addAttribute("testRequestScope4","hello,testModelMap");
		return "success";
	}

	@RequestMapping("/testSession")
	public String testSession(HttpSession session){
		session.setAttribute("testRequestScope5","hello Session");
		return "success";
	}

	@RequestMapping("/testApplication")
	public String testApplication(HttpSession session){
		ServletContext servletContext = session.getServletContext();
		servletContext.setAttribute("testRequestScope6","hello application");
		return "success";
	}

	@RequestMapping("/testForward")
	public String testForward(){
		return "";
	}
}
